package com.cskaoyan.javase.thread._6pool;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * newFixedThreadPool演示代码
 * @since 16:01
 * @author wuguidong@cskaoyan.onaliyun.com
 */
public class Demo2 {
    public static void main(String[] args) {
        //使用static ExecutorService newFixedThreadPool(int 个数)方法
        //返回值是一个线程池对象es
        //这个线程池对象里有2个线程
        ExecutorService es = Executors.newFixedThreadPool(2);
        Future<?> result = es.submit(new Task());
        try {
            // 提交Runnable任务没有结果,无需处理Future
            Object o = result.get();
            System.out.println(o);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        // es.submit(new Task());
        // 继续提交第三个任务
        // es.submit(new Task());
    }
}

class Task implements Runnable {
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + "执行了任务!");
    }
}